<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="跟进方式" prop="followMethod">
        <el-select v-model="queryParams.followMethod" placeholder="跟进方式">
          <el-option
            v-for="item in followMethodOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="跟进时间" prop="followTime">
        <el-date-picker clearable
          v-model="queryParams.followTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择跟进时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:follow:add']"
        >新增</el-button>
      </el-col>
     <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:follow:update']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:follow:delete']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:follow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="followList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="跟进方式" align="center" prop="followMethod" />
      <el-table-column label="跟进时间" align="center" prop="followTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.followTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="跟进人名称" align="center" prop="followUserName" />
      <el-table-column label="跟进内容" align="center" prop="followContent" width="280" />
      <el-table-column label="位置" align="center" prop="position" />
      <el-table-column label="下次跟进时间" align="center" prop="nextFollowTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nextFollowTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-detail"
            @click="handleDetail(scope.row)"
            v-hasPermi="['module:follow:id']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户跟进对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" readonly placeholder="请选择客户名称">
            <template slot="append">
              <el-button type="primary" @click="openImportCustomer">
                选择客户
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="联系人">
          <label >{{contactName}}</label>&nbsp;&nbsp;
          <label>{{contactTelephone}}</label>
        </el-form-item>
        <el-form-item label="跟进方式" prop="followMethod">
          <el-select v-model="form.followMethod" placeholder="跟进方式">
            <el-option
              v-for="item in followMethodOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟进时间" prop="followTime">
          <el-date-picker clearable
            v-model="form.followTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择跟进时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="跟进内容" prop="followContent">
          <el-input v-model="form.followContent" type="textarea" placeholder="请输入跟进内容" />
        </el-form-item>

        <el-form-item label="图片">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="附件" prop="enclosure">
          <image-upload v-model="form.enclosure"/>
        </el-form-item>
        <el-form-item label="位置" prop="position">
          <el-input v-model="form.position" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="下次跟进时间" prop="nextFollowTime">
          <el-date-picker clearable
            v-model="form.nextFollowTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择下次跟进时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 客户跟进详情 -->

    <el-dialog :title="detailTitle" :visible.sync="detailOpen" width="500px" append-to-body>
      <el-form ref="form" :model="detail"  label-width="100px">
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="detail.customerName" disabled >
          </el-input>
        </el-form-item>
        <el-form-item label="跟进人" prop="followUserName">
          <el-input v-model="detail.followUserName" disabled >
          </el-input>
        </el-form-item>
        <el-form-item label="跟进方式" prop="followMethod">
          <el-select v-model="detail.followMethod" disabled  placeholder="跟进方式">
            <el-option
              v-for="item in followMethodOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跟进时间" prop="followTime">
          <el-date-picker clearable disabled
                          v-model="detail.followTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="跟进内容" prop="followContent">
          <el-input v-model="detail.followContent" type="textarea" disabled placeholder="" />
        </el-form-item>
        <el-form-item label="图片">
          <img v-if="detail.image" :src="detail.imageShow" class="avatar" height="120">
        </el-form-item>
        <el-form-item label="附件" prop="enclosure">
          <img v-if="detail.enclosure" :src="detail.enclosureShow" class="avatar" height="120">
        </el-form-item>
        <el-form-item label="位置" prop="position">
          <el-input v-model="detail.position" disabled placeholder="" />
        </el-form-item>
        <el-form-item label="下次跟进时间" prop="nextFollowTime">
          <el-date-picker clearable disabled
                          v-model="detail.nextFollowTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择下次跟进时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </el-dialog>
    <import-customer ref="import" @showCustomer="showCustomer"></import-customer>
  </div>
</template>

<script>
import { listFollow, getFollow, delFollow, addFollow, updateFollow } from "@/api/module/follow";
import {getDicts} from "@/api/system/dict/data";
import importCustomer from "./customer";

export default {
  name: "Follow",
  components: { importCustomer },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户跟进表格数据
      followList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      detailTitle: "",
      // 是否显示弹出层
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        customerName: null,
        followMethod: null,
        followTime: null,
        followUserId: null,
        followUserName: null,
        image: null,
        enclosure: null,
        position: null,
        createId: null,
        nextFollowTime: null
      },
      // 表单参数
      form: {},
      detail:{},
      // 表单校验
      rules: {
      },
      followMethodOptions: [], // 跟进方式
      contactName : "",
      contactTelephone:""
    };
  },
  created() {
    this.getList();
    this.getFollowMethodOptionsList();
  },
  methods: {
    /** 查询客户跟进列表 */
    getList() {
      this.loading = true;
      listFollow(this.queryParams).then(response => {
        this.followList = response.rows;
        let arr = this.followList;
        arr.forEach(item => {
          item.followMethod = this.getValue(this.followMethodOptions, item.followMethod)
        });
        this.followList = arr;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerId: null,
        customerName: null,
        followMethod: null,
        followTime: null,
        followUserId: null,
        followUserName: null,
        image: null,
        enclosure: null,
        position: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        createId: null,
        nextFollowTime: null
      };
      this.resetForm("form");
      this.contactName="";
      this.contactTelephone="";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户跟进";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFollow(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户跟进";
      });
    },
    /** 详情 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getFollow(id).then(response => {
        this.detail = response.data;
        this.detail.imageShow=process.env.VUE_APP_BASE_API +response.data.image;
        this.detail.enclosureShow=process.env.VUE_APP_BASE_API +response.data.enclosure;
        this.detailOpen = true;
        this.detailTitle = "查看客户跟进";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFollow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFollow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户跟进编号为"' + ids + '"的数据项？').then(function() {
        return delFollow(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/follow/export', {
        ...this.queryParams
      }, `follow_${new Date().getTime()}.xlsx`)
    },
    // 获取跟进方式
    getFollowMethodOptionsList() {
      getDicts('biz_followmethod').then(response => {
        this.followMethodOptions = response.data
      })
    },
    getValue(arr, item) {
      for (let i = 0; i < arr.length; i++) {
        if (item === arr[i].dictValue) {
          return arr[i].dictLabel
        }
      }
    },

    /** 打开负责人弹窗 */
    openImportCustomer() {
      this.$refs.import.show();
    },
    showCustomer(data){
      this.form.customerId=data.customerId;
      this.form.customerName=data.customerName;
      this.contactName=data.contactName;
      this.contactTelephone=data.contactTelephone;

    }
  }
};
</script>
